package com.suntianci.stock.controller;

import com.suntianci.stock.pojo.entity.SysRole;
import com.suntianci.stock.service.RoleService;
import com.suntianci.stock.vo.req.AddRoleReqVo;
import com.suntianci.stock.vo.req.RolesInfoReqVo;
import com.suntianci.stock.vo.req.UpdateRoleReqVo;
import com.suntianci.stock.vo.resp.PageResult;
import com.suntianci.stock.vo.resp.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName RoleController
 * @Author 孙天赐
 * @Date 2025/1/11 10:36
 * @Description TODO: 角色相关接口控制器
 */

@RestController
@RequestMapping("/api")
@Api(tags = "角色相关接口处理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色信息
     *
     * @param rolesInfoReqVo
     * @return
     */
    @ApiOperation(value = "分页查询角色信息", notes = "分页查询角色信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolesInfoReqVo", value = "角色管理请求参数封装", dataType = "RolesInfoReqVo", required = true, type = "query")
    })
    @PostMapping("/roles")
    public R<PageResult<SysRole>> getRolesByPage(@RequestBody RolesInfoReqVo rolesInfoReqVo) {
        return roleService.getRolesByPage(rolesInfoReqVo);
    }

    /**
     * 根据角色id删除角色信息
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色id删除角色信息", notes = "根据角色id删除角色信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", required = true, type = "path")
    @DeleteMapping("/role/{roleId}")
    public R<Void> deleteById(@PathVariable String roleId){
        return roleService.deleteById(roleId);
    }

    /**
     * 更新角色的状态信息
     * @param roleId
     * @param status
     * @return
     */
    @ApiOperation(value = "更新角色的状态信息", notes = "更新角色的状态信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", required = true, type = "path"),
            @ApiImplicitParam(name = "status", value = "角色状态", dataType = "int", required = true, type = "path")
    })
    @PostMapping("/role/{roleId}/{status}")
    public R<Void> updateRoleStatusById(@PathVariable String roleId, @PathVariable Integer status){
        return roleService.updateRoleStatusById(roleId, status);
    }

    /**
     * 新增角色
     * @param addRoleReqVo
     * @return
     */
    @ApiOperation(value = "更新角色的状态信息", notes = "更新角色的状态信息", httpMethod = "POST")
    @ApiImplicitParam(name = "addRoleReqVo", value = "添加角色请求实体Vo类", dataType = "AddRoleReqVo", required = true, type = "query")
    @PostMapping("/role")
    public R<Void> addRole(@RequestBody AddRoleReqVo addRoleReqVo){
        return roleService.addRole(addRoleReqVo);
    }

    /**
     * 根据角色id查找对应的权限id集合
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色id查找对应的权限id集合", notes = "根据角色id查找对应的权限id集合", httpMethod = "GET")
    @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", required = true, type = "path")
    @GetMapping("/role/{roleId}")
    public R<List<String>> getAllPermissionByRoleId(@PathVariable String roleId){
        return roleService.getAllPermissionByRoleId(roleId);
    }

    /**
     * 更新角色信息，包含角色关联的权限信息
     * @param updateRoleReqVo
     * @return
     */
    @ApiOperation(value = "更新角色信息，包含角色关联的权限信息", notes = "更新角色信息，包含角色关联的权限信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "updateRoleReqVo", value = "更新角色信息请求Vo类", dataType = "UpdateRoleReqVo", required = true, type = "query")
    @PutMapping("/role")
    public R<Void> updateRole(@RequestBody UpdateRoleReqVo updateRoleReqVo){
        return roleService.updateRole(updateRoleReqVo);
    }

}
